package cn.lpz.tst;

import cn.lpz.dao.IAccountDao;
import cn.lpz.domian.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Testmybatis {
    @Test
    public void tets() throws IOException {
        InputStream in = Resources.getResourceAsStream("ibatisConfig.xml");
        //2创建工厂对象 构建者设计模式
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        //3利用工厂生产对象
        SqlSession sqlSession = factory.openSession();
        //4获取代理对象
        IAccountDao mapper = sqlSession.getMapper(IAccountDao.class);
        List<Account> all = mapper.findAll();
        for (Account account:all){
            System.out.println(account);
        }
        sqlSession.close();
        in.close();
    }
    @Test
    public void tets2() throws IOException {
        InputStream in = Resources.getResourceAsStream("ibatisConfig.xml");
        //2创建工厂对象 构建者设计模式
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        //3利用工厂生产对象
        SqlSession sqlSession = factory.openSession(true);
        //4获取代理对象
        IAccountDao mapper = sqlSession.getMapper(IAccountDao.class);
        Account account=new Account();
        account.setMoney(45f);
        account.setName("真实的");
        mapper.saveAccount(account);
        sqlSession.close();
        in.close();
    }

}
